// pages/order/order-detail.js
const format = require('../../utils/format.js')
const app = getApp()
const tabs = [
    { name: "待配送", status: 20 },
    { name: "已发货", status: 21 },
    { name: "待自提", status: 11 },
    { name: "已完成", status: 40 },
]
const orderActions = [
    {
    name: "确认配送",
    method: "doOrder",
    type: 1,
    style:''
},
{
    name: "确认到货",
    method: "doOrder",
    type: 2,
    style: '' 
},
{
    name: "扫码提货",
    method: "scanCode",
    type: 3,
    style: 'color:#ff5400;background-color:#fff;'    
},
{
    name: "取件码提货",
    method: "showPutCode",
    type: 3,
    style: ''    
},
]

function findAction() {
    return orderActions.filter(o => {
        return Array.from(arguments).indexOf(o.name) >= 0
    })
}

function getOrderAction(item) {
    const status = parseInt(item.status)
    let r = []
    switch (status) {
        case 20:
            break
        case 21:
            r = findAction("确认到货")
            break
        case 11:
            r = findAction("扫码提货", "取件码提货")
            break
        case 40:
            break
    }
    return r
}

Page({

    data: {
        
    },

    onLoad: function(options) {
        this.setData(options)
        let statusObj = {}
        Object.keys(tabs).forEach(k => {
            statusObj[tabs[k].status] = tabs[k].name
        })
        this.setData({
            statusObj,
            tabs
        })
        this.loadData()
    },
    loadData() {
        wx.showLoading({
            title: '加载中...',
        })
        app.http.post('administrator/detail', {
            data: {
                order_id: this.data.order_id
            }
        }).then(res => {
            res.data.actions = getOrderAction(res.data)
            this.setData(res.data)
            wx.hideLoading()
        })
    },
    //操作按钮
    doOrder(evt) {
        let { submittype } = evt.currentTarget.dataset
        wx.showLoading({
            title: '请稍等...',
        })
        let data = {
            order_id: this.data.order_id,
            type: submittype
        }
        if (this.data.take_code) {
            this.setData({
                showPutCode: false
            })
            data.take_code = this.data.take_code
        }
        app.http.get('administrator/status', {
            data
        }).then(res => {
            wx.hideLoading()
            if (res.data.status == 1) {
                wx.showToast({
                    icon: 'none',
                    title: '操作成功',
                })
                this.loadData()
            } else {
                wx.showModal({
                    title: '小提示',
                    content: res.data.msg,
                    showCancel: false,
                    confirmColor: '#ff5400'
                })
            }
        }).catch(err => {
            wx.hideLoading()
        })

    },
    //扫二维码
    scanCode(evt) {
        let {
            submittype
        } = evt.currentTarget.dataset
        wx.scanCode({
            success: res => {
                this.setData({
                    take_code: res.result,
                })
                this.doOrder({
                    currentTarget: {
                        dataset: {
                            submittype,
                        }
                    }
                })
            }
        })
    },
    //弹出取件码输入框
    showPutCode(evt) {
        let {
            submittype,
        } = evt.currentTarget.dataset
        let obj = {
            showPutCode: !this.data.showPutCode,
        }
        if (submittype) {
            obj.submittype = submittype
        }
        this.setData(obj)
    },
    //取件码取件
    putCodeSubmit(evt) {
        let take_code = evt.detail.value.take_code
        this.setData({
            take_code
        })
        this.doOrder({
            currentTarget: {
                dataset: {
                    submittype: this.data.submittype,
                }
            }
        })

    },
})